﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
//using Model;

/// <summary>
/// Description résumée de ActiviteManager
/// </summary>
/// 
namespace DAL
{
    public class ActiviteManager : IActiviteManager
    {
        private IAATOEntities dbService;

        public ActiviteManager()
        {
            dbService = new IAATOEntities();
        }

        public bool addActivite(ACTIVITE activite)
        {
            try
            {
                dbService.ACTIVITEJeu.AddObject(activite);
                dbService.SaveChanges();

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool deleteActivite(int activiteId)
        {
            try
            {
                ACTIVITE activite = (from a in dbService.ACTIVITEJeu
                                 where a.ID == activiteId
                                 select a).First();

                dbService.DeleteObject(activite);
                dbService.SaveChanges();
                return true;
            }

            catch (Exception ex)
            {
                return false;
            }

        }

        public List<ACTIVITE> getAllActivites()
        {
            try
            {
                return (from a in dbService.ACTIVITEJeu
                        orderby a.NOM
                        select a).ToList();
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public bool updateActivite()
        {
            try
            {
                dbService.SaveChanges();
                return true;
            }

            catch (Exception ex)
            {
                return false;
            }
        }

        public ACTIVITE getActiviteById(int id)
        {
            try
            {
                return (from a in dbService.ACTIVITEJeu
                        where a.ID == id
                        select a).First();
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public List<ACTIVITE> getActivitesOnSite(int siteID)
        {
            try
            {
                List<int> activitesID = (from l in dbService.ACTIVITE_SITE_LINKJeu
                        where l.SITEID == siteID
                        orderby l.ACTIVITEID
                        select l.ACTIVITEID).ToList();

                return (from a in dbService.ACTIVITEJeu
                        where activitesID.Contains(a.ID)
                            select a).ToList();
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public bool addActiviteOnSite(int siteID, int activiteID)
        {
            try
            {
                ACTIVITE_SITE_LINK link = new ACTIVITE_SITE_LINK();
                link.ACTIVITEID = activiteID;
                link.SITEID = siteID;

                dbService.ACTIVITE_SITE_LINKJeu.AddObject(link);
                dbService.SaveChanges();

                return true;
            }

            catch (Exception ex)
            {
                return false;
            }
        }

        public bool removeActiviteOnSite(int siteID, int activiteID)
        {
            try
            {
                ACTIVITE_SITE_LINK link = (from l in dbService.ACTIVITE_SITE_LINKJeu
                                           where l.ACTIVITEID == activiteID && l.SITEID == siteID
                                           select l).First();

                dbService.DeleteObject(link);
                dbService.SaveChanges();
                return true;
            }

            catch (Exception ex)
            {
                return false;
            }
        }

        public string getNom(int id)
        {
            try
            {
                return (from a in dbService.ACTIVITEJeu
                        where a.ID == id
                        select a.NOM).First();
            }
            catch (Exception ex)
            {
                return null;
            }
        }


        public ACTIVITE getActiviteByNom(string nom)
        {
            try
            {
                return (from a in dbService.ACTIVITEJeu
                        where a.NOM == nom
                        select a).First();
            }
            catch (Exception ex)
            {
                return null;
            }
        }
    }
}